/// 媒体类型枚举
enum MediaType {
  image('image'),
  video('video');

  const MediaType(this.value);
  final String value;
}

/// 风筝时刻实体模型
class KiteMomentEntity {
  /// 唯一标识符
  final String uniqueIdentifier;

  /// 标题
  final String momentTitle;

  /// 详情文字
  final String contentText;

  /// 发布者ID
  final String authorIdentifier;

  /// 媒体类型
  final MediaType mediaType;

  /// 资源文件路径
  final String mediaResourceUrl;

  /// 缩略图路径
  final String? thumbnailResourceUrl;

  /// 发布时间
  final DateTime publishTimestamp;

  /// 评论数量
  final int commentCount;

  const KiteMomentEntity({
    required this.uniqueIdentifier,
    required this.momentTitle,
    required this.contentText,
    required this.authorIdentifier,
    required this.mediaType,
    required this.mediaResourceUrl,
    this.thumbnailResourceUrl,
    required this.publishTimestamp,
    required this.commentCount,
  });

  /// 创建空实例
  factory KiteMomentEntity.createEmptyInstance() {
    return KiteMomentEntity(
      uniqueIdentifier: '',
      momentTitle: '',
      contentText: '',
      authorIdentifier: '',
      mediaType: MediaType.image,
      mediaResourceUrl: '',
      thumbnailResourceUrl: null,
      publishTimestamp: DateTime.now(),
      commentCount: 0,
    );
  }

  /// 从Map数据创建实例
  factory KiteMomentEntity.fromDataMap(Map<String, dynamic> dataMap) {
    return KiteMomentEntity(
      uniqueIdentifier: dataMap['uniqueIdentifier']?.toString() ?? '',
      momentTitle: dataMap['momentTitle']?.toString() ?? '',
      contentText: dataMap['contentText']?.toString() ?? '',
      authorIdentifier: dataMap['authorIdentifier']?.toString() ?? '',
      mediaType: MediaType.values.firstWhere(
        (type) => type.value == dataMap['mediaType'],
        orElse: () => MediaType.image,
      ),
      mediaResourceUrl: dataMap['mediaResourceUrl']?.toString() ?? '',
      thumbnailResourceUrl: dataMap['thumbnailResourceUrl']?.toString() ?? '',
      publishTimestamp:
          DateTime.tryParse(dataMap['publishTimestamp']?.toString() ?? '') ??
          DateTime.now(),
      commentCount: dataMap['commentCount'] ?? 0,
    );
  }

  /// 转换为Map数据
  Map<String, dynamic> convertToDataMap() {
    return {
      'uniqueIdentifier': uniqueIdentifier,
      'momentTitle': momentTitle,
      'contentText': contentText,
      'authorIdentifier': authorIdentifier,
      'mediaType': mediaType.value,
      'mediaResourceUrl': mediaResourceUrl,
      'thumbnailResourceUrl': thumbnailResourceUrl,
      'publishTimestamp': publishTimestamp.toIso8601String(),
      'commentCount': commentCount,
    };
  }

  /// 复制实例并修改指定属性
  KiteMomentEntity duplicateWithModifications({
    String? uniqueIdentifier,
    String? momentTitle,
    String? contentText,
    String? authorIdentifier,
    MediaType? mediaType,
    String? mediaResourceUrl,
    String? thumbnailResourceUrl,
    DateTime? publishTimestamp,
    int? commentCount,
  }) {
    return KiteMomentEntity(
      uniqueIdentifier: uniqueIdentifier ?? this.uniqueIdentifier,
      momentTitle: momentTitle ?? this.momentTitle,
      contentText: contentText ?? this.contentText,
      authorIdentifier: authorIdentifier ?? this.authorIdentifier,
      mediaType: mediaType ?? this.mediaType,
      mediaResourceUrl: mediaResourceUrl ?? this.mediaResourceUrl,
      thumbnailResourceUrl: thumbnailResourceUrl ?? this.thumbnailResourceUrl,
      publishTimestamp: publishTimestamp ?? this.publishTimestamp,
      commentCount: commentCount ?? this.commentCount,
    );
  }

  /// 检查实例是否为空
  bool validateEmptyState() {
    return uniqueIdentifier.isEmpty || momentTitle.isEmpty;
  }

  /// 是否为图片类型
  bool get isImageType => mediaType == MediaType.image;

  /// 是否为视频类型
  bool get isVideoType => mediaType == MediaType.video;

  /// 获取显示用的缩略图路径
  String get displayThumbnailUrl {
    if (thumbnailResourceUrl != null && thumbnailResourceUrl!.isNotEmpty) {
      return thumbnailResourceUrl!;
    }
    return isImageType ? mediaResourceUrl : '';
  }

  @override
  String toString() {
    return 'KiteMomentEntity{id: $uniqueIdentifier, title: $momentTitle, type: ${mediaType.value}}';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is KiteMomentEntity &&
        other.uniqueIdentifier == uniqueIdentifier;
  }

  @override
  int get hashCode {
    return uniqueIdentifier.hashCode;
  }

  /// 预定义的风筝时刻数据集合
  static final List<KiteMomentEntity> _predefinedMomentCollection = [
    // 图片类型的风筝时刻（9个）
    KiteMomentEntity(
      uniqueIdentifier: 'moment_img_002',
      momentTitle: 'Family Kite Time: Simple Joys Under the Sun',
      contentText:
          'Some of the best moments are the simplest—like this evening, flying a kite with my people in the golden hour. The kiddo’s eyes lit up watching it dance in the wind, and honestly? My heart did too. No fancy plans, just grass, sun, and a kite that felt like it was holding all our laughter. 🌅🪁',
      authorIdentifier: 'kite_user_002',
      mediaType: MediaType.image,
      mediaResourceUrl: 'assets/images/kite_moments/traditional_kite_02.png',
      publishTimestamp: DateTime.now().subtract(const Duration(days: 5)),
      commentCount: 4,
    ),
    KiteMomentEntity(
      uniqueIdentifier: 'moment_img_003',
      momentTitle: 'Little Hands, Big Kite Dreams',
      contentText:
          'Watching him run after the kite, tiny hand gripping the string like it’s holding the world. Reminds me that joy is simple—just a colorful kite, open sky, and the courage to let it fly. Today, we’re not just flying a kite; we’re teaching a little heart to chase big dreams. 👦🪁 #KidAdventures #GrowWithKites',
      authorIdentifier: 'kite_user_003',
      mediaType: MediaType.image,
      mediaResourceUrl: 'assets/images/kite_moments/festival_kites_03.png',
      publishTimestamp: DateTime.now().subtract(const Duration(days: 7)),
      commentCount: 3,
    ),

    KiteMomentEntity(
      uniqueIdentifier: 'moment_img_001',
      momentTitle: 'Kite Meets Couture: My Colorful Adventure',
      contentText:
          'Who says kites can’t be high fashion? Paired my patchwork coat (yes, it’s as comfy as it looks) with a rainbow kite for a day of chasing wind. Turns out, the sky’s the perfect runway—no audience, just me, my kite, and endless color. Fashion should be fun, right? 🎨🪁',
      authorIdentifier: 'kite_user_001',
      mediaType: MediaType.image,
      mediaResourceUrl: 'assets/images/kite_moments/sunset_kite_01.png',
      publishTimestamp: DateTime.now().subtract(const Duration(days: 2)),
      commentCount: 5,
    ),
    KiteMomentEntity(
      uniqueIdentifier: 'moment_img_004',
      momentTitle: 'When Fashion Meets Flight: My Parrot Kite Moment',
      contentText:
          'Found my spirit animal in kite form—this vibrant parrot! Paired it with a flowy dress and headed to the beach, where the wind and waves felt like they were cheering us on. Fashion isn’t just about clothes; it’s about embracing the wild, colorful energy of the world. Today, I’m not just wearing a dress—I’m wearing the freedom to soar. 🌊🪁  ',
      authorIdentifier: 'kite_user_004',
      mediaType: MediaType.image,
      mediaResourceUrl: 'assets/images/kite_moments/box_kite_04.png',
      publishTimestamp: DateTime.now().subtract(const Duration(days: 10)),
      commentCount: 5,
    ),
    KiteMomentEntity(
      uniqueIdentifier: 'moment_img_005',
      momentTitle: 'Chasing Sunsets and Kite Strings as a Family',
      contentText:
          'There’s magic in ending the day this way—feet in the sand, a kite dancing with the last light of the sun. The ocean hums, the kiddo giggles, and suddenly, all the noise fades. This is what ‘home’ feels like, even when we’re miles from it. Grateful for these quiet, golden hours. 🌊🪁',
      authorIdentifier: 'kite_user_005',
      mediaType: MediaType.image,
      mediaResourceUrl: 'assets/images/kite_moments/beach_kite_05.png',
      publishTimestamp: DateTime.now().subtract(const Duration(days: 12)),
      commentCount: 3,
    ),
    KiteMomentEntity(
      uniqueIdentifier: 'moment_img_006',
      momentTitle: 'Chasing Freedom, One Kite String at a Time',
      contentText:
          'There’s something about letting a kite go that feels like letting go of everything else—worries, plans, expectations. Out here, with the sun on my face and the wind in my hands, I’m not just flying a kite. I’m reminding myself that freedom isn’t something you find—it’s something you chase. And today? I’m winning. 🌞🪁',
      authorIdentifier: 'kite_user_006',
      mediaType: MediaType.image,
      mediaResourceUrl: 'assets/images/kite_moments/led_kite_06.png',
      publishTimestamp: DateTime.now().subtract(const Duration(days: 15)),
      commentCount: 4,
    ),
    KiteMomentEntity(
      uniqueIdentifier: 'moment_img_007',
      momentTitle: 'Learning to Let Go (With a Little Help From a Kite',
      contentText:
          'Today, she learned that sometimes, to let your dreams fly, you’ve got to loosen the string a little. Watched her face light up as the kite soared—proof that growing up isn’t just about holding on, but knowing when to let things rise. So proud of my little student of life. 🌬️🪁',
      authorIdentifier: 'kite_user_007',
      mediaType: MediaType.image,
      mediaResourceUrl: 'assets/images/kite_moments/kids_kite_07.png',
      publishTimestamp: DateTime.now().subtract(const Duration(days: 18)),
      commentCount: 9,
    ),
    KiteMomentEntity(
      uniqueIdentifier: 'moment_img_008',
      momentTitle: 'Sunset Kites and Sea Breezes: My Kind of Therapy',
      contentText:
          'There’s a peace in chasing a kite as the sun dips below the sea—like the world slows down just for me. The salt air, the gentle pull of the string, and colors melting into the sky… Reminds me that beauty doesn’t need words. It just needs to be felt. 🌅🪁',
      authorIdentifier: 'kite_user_008',
      mediaType: MediaType.image,
      mediaResourceUrl: 'assets/images/kite_moments/octopus_kite_08.png',
      publishTimestamp: DateTime.now().subtract(const Duration(days: 20)),
      commentCount: 25,
    ),

    KiteMomentEntity(
      uniqueIdentifier: 'moment_img_009',
      momentTitle: 'Little Explorer, Big Kite Dreams',
      contentText:
          'Watching him out here, tiny hands guiding that colorful kite—feels like he’s steering a dragon across the sky. The beach, the breeze, the endless wonder in his eyes… childhood magic at its best. Today, he’s not just flying a kite; he’s chasing adventures only a kid could dream up. 🏖️🪁 ',
      authorIdentifier: 'kite_user_001',
      mediaType: MediaType.image,
      mediaResourceUrl: 'assets/images/kite_moments/beach_kite_09.png',
      publishTimestamp: DateTime.now().subtract(const Duration(days: 12)),
      commentCount: 7,
    ),

    // 视频类型的风筝时刻（2个）
    KiteMomentEntity(
      uniqueIdentifier: 'moment_vid_001',
      momentTitle: 'Acrobatic Kite Performance',
      contentText:
          'Watch my dual-line sport kite perform loops and dives in perfect synchronization with the music!',
      authorIdentifier: 'kite_user_002',
      mediaType: MediaType.video,
      mediaResourceUrl: 'assets/videos/video1.mp4',
      thumbnailResourceUrl: 'assets/images/video1.png',
      publishTimestamp: DateTime.now().subtract(const Duration(days: 3)),
      commentCount: 18,
    ),
    KiteMomentEntity(
      uniqueIdentifier: 'moment_vid_002',
      momentTitle: 'Kite Crash and Recovery',
      contentText:
          'Sometimes things don\'t go as planned! Here\'s how I rescued my kite from a tree.',
      authorIdentifier: 'kite_user_003',
      mediaType: MediaType.video,
      mediaResourceUrl: 'assets/videos/video2.mp4',
      thumbnailResourceUrl: 'assets/images/video2.png',
      publishTimestamp: DateTime.now().subtract(const Duration(days: 8)),
      commentCount: 14,
    ),
  ];

  /// 根据ID查找预定义的风筝时刻数据
  static KiteMomentEntity? findMomentByIdentifier(String targetId) {
    try {
      return _predefinedMomentCollection.firstWhere(
        (moment) => moment.uniqueIdentifier == targetId,
      );
    } catch (e) {
      return null;
    }
  }

  /// 获取所有预定义的风筝时刻数据
  static List<KiteMomentEntity> getAllPredefinedMoments() {
    return List.unmodifiable(_predefinedMomentCollection);
  }

  /// 根据作者ID获取风筝时刻
  static List<KiteMomentEntity> getMomentsByAuthor(String authorId) {
    return _predefinedMomentCollection
        .where((moment) => moment.authorIdentifier == authorId)
        .toList();
  }

  /// 根据媒体类型获取风筝时刻
  static List<KiteMomentEntity> getMomentsByMediaType(MediaType mediaType) {
    return _predefinedMomentCollection
        .where((moment) => moment.mediaType == mediaType)
        .toList();
  }

  /// 获取预定义数据的总数
  static int getPredefinedMomentCount() {
    return _predefinedMomentCollection.length;
  }
}
